package com.cargo.base.servlet;

import com.cargo.base.exceptions.CommonException;
import com.cargo.base.servlet.bean.BaseRequestBean;
import com.cargo.base.utils.CheckUtil;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class CommonServlet implements Servlet {
	
	private static final Logger log = Logger.getLogger(CommonServlet.class);
	
	private WebApplicationContext wac;
	
	private ITransmission transmission;

	@Override
	public void destroy() {
		
	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		wac = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
		transmission = (ITransmission) wac.getBean("transmissionBean");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		HttpServletRequest request = (HttpServletRequest) req;
		
		HttpServletResponse response = (HttpServletResponse) resp;
		try {
			
			if(CheckUtil.checkObj(transmission)){
				
				String reqJson = request.getParameter("body");
				
				log.info("===请求的json===>>" + reqJson);
				
				String transCode = getTransCode(reqJson, "transCode");
				
				log.info("=接口号transCode=>"+transCode);
				
				LogincFace loginc = (LogincFace)wac.getBean(transCode);
				
				Class<?> Reqlass = loginc.getReqClass();
				
				String content = getElementByKey(reqJson, "content");
				
				Gson gson = new Gson();
				BaseRequestBean brb = (BaseRequestBean) gson.fromJson(content, Reqlass);
				
				returnMap = (Map<String,Object>) loginc.logic(brb);
				
				//发送业务请求返回结果
				transmission.resp(response, returnMap);
				
			}
			
		}catch(CommonException e){
			returnMap.put("flag", "-1");
			returnMap.put("meg", e.getMessage());
			transmission.resp(response, returnMap);
		} catch (Exception e) {
			returnMap.put("flag", "-1");
			returnMap.put("meg", "发送失败");
			transmission.resp(response, returnMap);
		}
	}
	
	private String getTransCode(String s, String actionKey) throws Exception {
		String key = "";
		JsonReader jr = new JsonReader(new StringReader(s));
		try {
			jr.beginObject();
			while (jr.hasNext()) {
				String keyName = jr.nextName();
				if (actionKey.equals(keyName)) {
					key = jr.nextString();
				} else {
					jr.skipValue();
				}
			}
			jr.endObject();
		} catch (Exception e) {
			log.debug(e, e);
			throw e;
		}
		return key;
	}
	
	private static String getElementByKey(String json,String key) throws Exception{
		String element = "";
		
		JSONObject jsonObject = JSONObject.fromObject(json);
		
		Object value = jsonObject.get(key);
		
		if(CheckUtil.checkObj(value)){
			element = String.valueOf(value);
		}
		
		return element;
	}
	
}
